capture log close

clear all

set more off
set matsize 2000

cd ${path}appendix

log using smog_pollution_residuals.log, replace

/* This program plots trends in re-inspections: the share of old vehicles
and residuals of our re-inspection measures, for LA, SF and San Diego*/


use ../analysis/pollution_star_merged

xtset county date

/*First, divide the counts of inspections by 1000, to make the coefficients easier to read*/


foreach var of varlist firstgen* secondgen* thirdgen* othergen* re_pass*{
	qui replace `var' = (`var')/1000
}


/* Generate day-of-week and calendar week*/

gen dow = dow(date)
gen week = wofd(date)

/*Declare the "if" conditions in a local, as well as the full set of controls*/

local conditions  if year>=1998 & inlist(county,5,12,18,22,26,53)==0 & (inlist(county,3,6,11,12,13,17,23,47,58)==0 | (year !=2002&year!=2003)) 
local controls tMin tMax prec ib1.county ib1.county#(c.date##c.date##c.date)  i.week


regress firstgen_90  `controls' `conditions' 
predict firstgen_residuals, res

regress  othergen_90 `controls' `conditions' 
predict othergen_residuals, res

format date %tdCCYY

#delimit ;
twoway line firstgen_residuals date if county ==19 || line firstgen_residuals date if county == 38 ||
line firstgen_residuals date if county == 37 || if year >= 1998 & year <=2009 ,
tlabel(01jan1998(365)31dec2009) graphregion(color(white)) ttitle(Year)
legend(label(1 "Los Angeles County") label(2 "San Francisco County") label(3 "San Diego County")) 
;

graph export residuals_pre85.pdf, replace;

#delimit ;
twoway line othergen_residuals date if county ==19 || line othergen_residuals date if county == 38 ||
line othergen_residuals date if county == 37 || if year >= 1998 & year <=2009 ,
tlabel(01jan1998(365)31dec2009) graphregion(color(white))  ttitle(Year)
legend(label(1 "Los Angeles County") label(2 "San Francisco County") label(3 "San Diego County")) 
;
 
graph export residuals_post85.pdf, replace;

/* Also plot the share of all re-inspections that are older 1975-1985 vehicles*/

gen firstgen_share = firstgen/re_pass;

#delimit ;
twoway lowess firstgen_share date if county ==19 || lowess firstgen_share date if county == 38 ||
lowess firstgen_share date if county == 37 ||  if year >= 1998 & year <=2009 ,
tlabel(01jan1998(365)31dec2009) graphregion(color(white)) ytitle(Proportion) ttitle(Year)
legend(label(1 "Los Angeles County") label(2 "San Francisco County") label(3 "San Diego County")) 
;

graph export share_pre85.pdf, replace;

